<?php

/*	LibFox - Simple library for XML-based backends.
	Copyright (C) 2008 LinFox Serviços em Informática LTDA.

	This file is part of LibFox.

	LibFox is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	LibFox is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>. */

function str_underline($string)
{
	return str_replace(' ', '_', $string);
}

function str_is_ip($string)
{
	if (eregi('^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}(/[0-9]{1,2}){0,1}$', $string))
		return true;
	else
		return false;
}

function xml_iterate($object, $key_name = false)
{
	if ($key_name) {
		$result = '<'.$key_name.'>';
	} else {
		$result = '';
	}

	foreach ($object as $key => $value)
	{
		if (!is_array($value) && !is_object($value))
		{
			$result .= '<'.str_underline($key).'>' .$value. '</'.str_underline($key).'>';
		}
	}
	
	if ($key_name) {
		$result .= '</'.$key_name.'>';
	}
 
	return $result;
}

function xml_iterate_recursive($object, $key_name = false)
{
	if ($key_name)
		$result = '<'.$key_name.'>';
	else
		$result = ''; 

	if (is_array($object) || is_object($object))
	{
		foreach ($object as $key => $value)
		{
			if (is_array($value) || is_object($value))
			{
				$result .= xml_iterate($value, $key);
			}
			else
			{
				$result .= '<'.str_underline($key).'>' .$value. '</'.str_underline($key).'>';
			}
		}
		
		if ($key_name)
			$result .= '</'.$key_name.'>';
 
		return $result;
	}
	else
	{
		return false;
	}
}

function get($property)
{
	if (isset($_POST[$property])) {
		if (get_magic_quotes_gpc() == 1) {
			$post = stripslashes($_POST[$property]);
		} else {
			$post = $_POST[$property];
		}
		$post = str_replace("\r","\n",$post);
		return $post;
	} elseif (isset($_GET[$property])) {
		if (get_magic_quotes_gpc() == 1) {
			$get = stripslashes($_GET[$property]);
		} else {
			$get = $_GET[$property];
		}
		$get = str_replace("\r","\n",$get);
		return $get;
	}
	else return false;
}

/**
* Looks up the ARP cache table and returns it in an array.
**/
function arp($arp_file = '/proc/net/arp')
{
	$arp = file($arp_file);
	$result = array();
	foreach($arp as $row_number => $row) {
		if ($row_number == '0') {
			$keys = split(" [ ]+", $row);
		} else {
			$values = split(" [ ]+", $row);
			foreach ($values as $key => $value) {
				$result[$row_number][str_underline(strtolower($keys[$key]))] = $value;
			}
		}
	}
	return $result;
}

?>